package jp.co.hcs.attendancemanagement.login.logic;

import java.sql.SQLException;

import jp.co.hcs.attendancemanagement.data.dao.AuthenticationDAO;
import jp.co.hcs.attendancemanagement.data.dao.EmployeeMasterDAO;
import jp.co.hcs.attendancemanagement.data.entity.Authentication;
import jp.co.hcs.attendancemanagement.data.entity.EmployeeMaster;
import jp.co.hcs.attendancemanagement.logic.LogicBase;
import jp.co.hcs.attendancemanagement.login.logic.dto.LoginDto;

/**
 * The Class LoginLogic.
 */
public class LoginLogic extends LogicBase {

  /** The auth dao. */
  private AuthenticationDAO authDao;

  /** The employee dao. */
  private EmployeeMasterDAO employeeDao;

  /**
   * Authenticate.
   * 
   * @param loginDto the login dto
   * @return the string
   */
  public String authenticate(LoginDto loginDto) {
    Authentication auth = null;
    try {
      auth = getAuthDao().selectAuthenticationByPrimaryKey(loginDto.getUsername());
    } catch (SQLException e) {
    }
    if (auth == null) {
      return null;
    }

    if (!auth.getPassword().equals(loginDto.getPassword())) {
      return null;
    }

    try {
      EmployeeMaster employeeLoggedIn =
          getEmployeeDao().selectEmployeeMasterByPrimaryKey(loginDto.getUsername());
      return employeeLoggedIn.getEmployeeId();
    } catch (SQLException e) {
      return null;
    }
  }

  /**
   * Gets the auth dao.
   * 
   * @return the auth dao
   */
  public AuthenticationDAO getAuthDao() {
    if (authDao == null) {
      authDao = daoFactory.getAuthenticationDAO();
    }

    return authDao;
  }

  /**
   * Sets the auth dao.
   * 
   * @param authDao the new auth dao
   */
  public void setAuthDao(AuthenticationDAO authDao) {
    this.authDao = authDao;
  }

  /**
   * Gets the employee dao.
   * 
   * @return the employee dao
   */
  public EmployeeMasterDAO getEmployeeDao() {
    if (employeeDao == null) {
      employeeDao = daoFactory.getEmployeeMasterDAO();
    }

    return employeeDao;
  }

  /**
   * Sets the employee dao.
   * 
   * @param employeeDao the new employee dao
   */
  public void setEmployeeDao(EmployeeMasterDAO employeeDao) {
    this.employeeDao = employeeDao;
  }

}
